from datetime import datetime
from extensions import db
import sqlalchemy as sa


class CategoryORM(db.Model):
    """新闻分类"""
    __tablename__ = "forum_category"

    id = sa.Column(sa.Integer, primary_key=True)  # 分类编号
    name = sa.Column(sa.String(64), nullable=False)  # 分类名

    create_at = sa.Column(sa.DateTime, default=datetime.now)  # 记录的创建时间
    update_at = sa.Column(sa.DateTime, default=datetime.now, onupdate=datetime.now)  # 记录的更新时间

    article_list = db.relationship('ArticleORM', backref='category', lazy='dynamic')
    # 通过category_id反向获取分类名

    def save(self):
        db.session.add(self)
        db.session.commit()

    def delete(self):
        db.session.delete(self)
        db.session.commit()